import java.sql.*;
import java.io.*;

public class FileIterator {
	Connect con; 
	private String head ;
	private String subHead ;
	private String name ;
	private String editable;
	private String tableName;
	private String visible;
	
	String field = "", displayName = "",edit="" , table="", vis="";
	int ii;
	int j=0;
	int jj=0;

	private void readColumn(FileReader fr) throws Exception {
		 head = "Table";
		 subHead = "Column";
		 name = "Header ";
		 editable = "Editable";
		 tableName = "Name";
		 visible = "Hidden";
		
		BufferedReader br = new BufferedReader(fr);
		String s = null;
		try {
			boolean isHead = false;
			boolean isSubHead = false;
			int ii=0;
			
			while ((s = br.readLine()) != null) {
				//System.out.print(s);
				int j=s.indexOf("//");
				if (j!=-1) {
					continue;
				}
				
				if (!isHead && !isSubHead) { // not Table

					int t = s.indexOf(head);
					if (t != -1) {
						isHead = true;						
					}
				} else if(isHead && !isSubHead) { // is Table , not Field
					int n = s.indexOf(tableName); // TextName
					//System.out.println("n="+n);
					if (n != -1) {
						table =  s.substring(s.indexOf("= ") + 3,
										s.indexOf(";") - 1);
						System.out.println("t="+table);
					}
					if (!isSubHead) { 
						int t = s.indexOf(subHead);
						if (t != -1) {
							isSubHead = true;							
						}
					}
					
				} else if (isHead && isSubHead) { // is Text, is Field
					    int nn = s.indexOf(name); //Column name
					    if (nn != -1) {
					    	displayName= s.substring(s.indexOf("= ") + 3,
									s.indexOf(";") - 1);
							System.out.println("d="+displayName);
					    }
					
						nn = s.indexOf(tableName); 
						if (nn != -1) {
							field= s.substring(s.indexOf("= ") + 3,
									s.indexOf(";") - 1);
							System.out.println("f="+field);
							
						}
						nn = s.indexOf(editable);
						if (nn != -1) {
							edit = s.substring(s.indexOf("= ") + 2,
									s.indexOf(";"));
							System.out.println("editable=="+edit);
						}
						nn = s.indexOf(visible);
						if (nn != -1) {
							vis = s.substring(s.indexOf("=") + 2,
									s.indexOf(";"));
							System.out.println("hidden=="+vis);
						}
						
						nn = s.indexOf("}");
						if (nn != -1){ //end Element
				            ii++;
				            System.out.println("ii="+ii);
				            if (ii==2){
				            	field = ""; displayName = "";edit="";table="";vis="";
				            	isHead = false;
				    			isSubHead = false;
				            	ii=0;
				            	continue;
				            }
							System.out.println("ready"+jj);
				            
				      		String 
				      		s1=table,
				      		s2=field.toUpperCase(),
				      		s3=displayName,
				      		s4=edit,
				      		s5=vis;
				      		jj++;
				      		
				      		con.insert(jj,s1,s2,s3,s4,s5);
				      		
				      		field = ""; displayName = "";edit="";vis="";
				      			  
				         } else {
				        	//System.out.println("clear11");
				        	 ii=0;
				         }
						
			 	}
				
             
			}
		} finally {
		   br.close();
		   System.out.println("column close");
		}
	}
	
	private void readFields(FileReader fr) throws Exception {
		 head = "Text";
		 subHead = "Field";
		 name = "Name";
		 editable = "Editable";
		
		BufferedReader br = new BufferedReader(fr);
		String s = null;
		try {
			boolean isHead = false;
			boolean isSubHead = false;
			
			int j=0;
			while ((s = br.readLine()) != null) {
				//System.out.print(s);
				
				j= s.indexOf("Table {");
				if (j!=-1) {
					break;
				}
				if (!isHead && !isSubHead) { // not Text

					int t = s.indexOf(head);
					if (t != -1) {
						isHead = true;						
					}
				} else if(isHead && !isSubHead) { // is Text , not Field
					int n = s.indexOf(name); // TextName
					//System.out.println("n="+n);
					if (n != -1) {
						displayName = displayName
								+ s.substring(s.indexOf("= ") + 3,
										s.indexOf(";") - 1);
						System.out.println("d="+displayName);
					}
					if (!isSubHead) { 
						int t = s.indexOf(subHead);
						if (t != -1) {
							isSubHead = true;							
						}
					}
					
				} else if (isHead && isSubHead) { // is Text, is Field
                        ii++;
                        System.out.println("ii="+ii);
						int nn = s.indexOf(name);
						if (nn != -1) {
							field = s.substring(s.indexOf("= ") + 3,
									s.indexOf(";") - 1);
							System.out.println("f="+field);
						}
						nn = s.indexOf(editable);
						if (nn != -1) {
							edit = s.substring(s.indexOf("= ") + 2,
									s.indexOf(";"));
							System.out.println("editable=="+edit);
						}
						
			 	}
				
                if (ii==5){
                  System.out.println("bef"); 	
            	  ii=0;//reset ii;
            	  isHead = false;
      			  isSubHead = false;
      			  System.out.println("bef2");
      			  
      			  String s1="field",
      			  s2=field.toUpperCase(),
      			  s3=displayName.toUpperCase(),
      			  s4=edit.toUpperCase();
      		      System.out.println("bef3");
      			  
      		     
      		     // con.insert(s1,s2,s3,s4);  
      			        			 
      			  field = "";
     			  displayName = "";
      			  
      		    }
			}
		} finally {
		   br.close();
		   System.out.println("close");
		}

	}

	public static void main(String[] main) throws Exception {

		FileReader fr = new FileReader("PURCHASECARD.edit");
		
		FileIterator fi = new FileIterator();
	    fi.con =  Connect.getInstance();
		//fi.readFields(fr);
		fi.readColumn(fr);
		

	}
}